package com.example.hibernate.demo.repository;

import com.example.hibernate.demo.entity.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {
    
    List<Project> findByStatus(Project.ProjectStatus status);
    
    List<Project> findByStartDateAfter(LocalDate date);
    
    List<Project> findByEndDateBefore(LocalDate date);
    
    @Query("SELECT p FROM Project p WHERE p.startDate <= :date AND (p.endDate IS NULL OR p.endDate >= :date)")
    List<Project> findActiveProjectsAtDate(LocalDate date);
    
    @Query("SELECT p FROM Project p JOIN p.employees e WHERE e.id = :employeeId")
    List<Project> findByEmployeeId(Long employeeId);
    
    @Query("SELECT p FROM Project p JOIN FETCH p.employees WHERE p.id = :id")
    Project findByIdWithEmployees(Long id);
}